package com.gxuwz.medical.domain.area;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.List;

import com.gxuwz.medical.dao.AreaDao;
import com.gxuwz.medical.database.DbUtil;
import com.gxuwz.medical.domain.menu.Menu;
import com.gxuwz.medical.vo.PageBean;

public class Area {
	private String areacode;
	private String areapcode;
	private String areaname;
	private int arealevel;
	private AreaDao areadao = new AreaDao();
	
	public List getListbyLevel(int level) throws SQLException{
		return areadao.queryByLevel(level);
	}
	public List getChildren(String areapcode) throws SQLException{
		return areadao.getChildren(areapcode);
	}
	
	public void autoSet() throws SQLException{//通过上一级自动生成行政区域编码以及行政区域级别
		if(this.areapcode!=""||this.areapcode!=null){
			String code_str="";
			int code_int=01;
			Area tmp = new Area();
			tmp.setAreacode(areapcode);
			tmp=tmp.query();
			this.arealevel=tmp.getArealevel()+1;//级别=父级+1;
			DbUtil db = new DbUtil();
			Connection conn = db.getConn();
			Statement stmt = conn.createStatement();
			ResultSet rs = null;
			String sql = "select max(areacode) from area where areacode like '"+this.areapcode+"%' and areacode!='"+this.areapcode+"'";
			rs=db.executeQuery(stmt, sql);
			if(rs.next()){
				if(rs.getString(1)!=null){
					code_str=rs.getString(1);
				}
			}
			if(code_str!=""){
			code_str = code_str.substring(code_str.length()-2, code_str.length());
			code_int =Integer.parseInt(code_str)+1;
			}
			code_str =this.areapcode+String.format("%02d", code_int);
			this.areacode = code_str;
		}
	
	}
	public void add() throws SQLException {
		areadao.add(this);
	}

	public void delete(String areacode) throws SQLException {
		areadao.delete(areacode);
	}

	public Area query() throws SQLException {
		return areadao.query(areacode);
	}

	public PageBean queryAll(int page,int size) throws SQLException {
		return areadao.queryAll(page,size);
	}
	public List<Area> queryAll() throws SQLException {
		return areadao.queryAll();
	}

	public void update() throws SQLException {
		areadao.update(this);
	}
	
	public String getAreacode() {
		return areacode;
	}
	public void setAreacode(String areacode) {
		this.areacode = areacode;
	}
	public String getAreapcode() {
		return areapcode;
	}
	public void setAreapcode(String areapcode) {
		this.areapcode = areapcode;
	}
	public String getAreaname() {
		return areaname;
	}
	public void setAreaname(String areaname) {
		this.areaname = areaname;
	}
	public int getArealevel() {
		return arealevel;
	}
	public void setArealevel(int arealevel) {
		this.arealevel = arealevel;
	}
	
}
